<?php

require_once('inc/addons.php');
require_once('inc/users.php');
require_once('inc/functions.php');

define('SUBPAGE', 'plugins');

Users::startSession();

if (isset($_GET['sort'])) {
	if ($_GET['sort'] == 'title' || $_GET['sort'] == 'author'
	|| $_GET['sort'] == 'verified' || $_GET['sort'] == 'rating') {
		define('SORT', $_GET['sort']);
	}
}

if (!defined('SORT')) { define('SORT', 'title'); }

define('ORDER', isset($_GET['desc']) ? 'DESC' : 'ASC');

function showSort($type) {
	echo '<a href="?sort=' . $type;

	if ($type == SORT && ORDER != 'DESC') {
		echo '&amp;desc';
	}

	echo '">' . ucfirst($type) . '</a>';
	if ($type == SORT) {
		echo ' &' . (ORDER == 'DESC' ? 'u' : 'd') . 'arr;';
	}
}

showHeader('Plugins');
?>
    <h2>Plugins</h2>
    <p>
     Plugins are compiled code that enable you to add new functionality to
     DMDirc.
    </p>
    <table style="width: 100%">
     <tr>
      <th><?php showSort('title'); ?></th>
      <th><?php showSort('author'); ?></th>
      <th><?php showSort('verified'); ?></th>
      <th><?php showSort('rating'); ?></th>
     </tr>
<?php

 $sql  = 'SELECT addon_id, addon_title, user_displayname AS addon_author, addon_verified, ';
 $sql .= 'addon_rating FROM addons NATURAL JOIN users WHERE addon_type = \'plugin\'';
 $sql .= ' ORDER BY addon_' . SORT . ' ' . ORDER;
 $res  = mysql_query($sql) or die(mysql_error());

 while ($row = mysql_fetch_assoc($res)) {
 	echo '<tr><td><a href="/addon/' . $row['addon_id'] . '">' . htmlentities($row['addon_title']) . '</a></td>';
 	echo '<td>' . htmlentities($row['addon_author']) . '</td>';
 	echo '<td>' . ucfirst($row['addon_verified']) . '</td>';
 	echo '<td>' . $row['addon_rating'] . '</td></tr>';
 }

?>
    </table>
<?php
	showFooter();
?>
